package com.example.administrator.caigou51.activity;

import android.app.usage.UsageEvents;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.example.administrator.caigou51.EventBusClass.JpushCurrentMessageActivity;
import com.example.administrator.caigou51.EventBusClass.JpushGoToMessage;
import com.example.administrator.caigou51.R;
import com.example.administrator.caigou51.bean.MainDataBean;
import com.example.administrator.caigou51.bean.MessageBean;
import com.example.administrator.caigou51.global.App;
import com.example.administrator.caigou51.global.Constant;
import com.example.administrator.caigou51.recyclerCard.basic.MaterialListView;
import com.example.administrator.caigou51.recyclerCard.card.MessageTypeCard;
import com.example.administrator.caigou51.request.BaseResponse;
import com.example.administrator.caigou51.request.ICallBackForRequestServer;
import com.example.administrator.caigou51.request.RequestServerManager;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2016/8/25/025.
 */
public class MessageActivity extends GBaseActivity {
    private ImageView imageViewHdtz;

    List<MessageBean.DataEntity> messageBeanList;
    MaterialListView material_listview;
    public static final String HDTZ_TAG="0";
    public static final String DDTX_TAG="1";
    public static final String GGTZ_TAG="2";
    View linearLayoutEmpty;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentViewFor720_1280(R.layout.activity_message_have_empty);
        findViewByIds();
        initDatas();
        TaskGetMessageList(false);
    }

    private void findViewByIds() {
        imageViewHdtz = (ImageView) findViewById(R.id.imageViewHdtz);
        material_listview= (MaterialListView) findViewById(R.id.material_listview);
        linearLayoutEmpty=findViewById(R.id.linearLayoutEmpty);
    }
    private void initDatas() {
        setTopTitle("消息");
        getRightImageView().setVisibility(View.GONE);
        EventBus.getDefault().register(this);

    }
    private void fillData( List<MessageBean.DataEntity> messageBeanList)
    {
        material_listview.clear();
        for (int i=0;i<messageBeanList.size();i++)
        {
            MessageTypeCard messageTypeCard=new MessageTypeCard(this);
            messageTypeCard.setDataEntity(messageBeanList.get(i));
            messageTypeCard.setMaterialListView(material_listview);
            material_listview.add(messageTypeCard);
        }

    }
    private void TaskGetMessageList(final boolean isloadmore) {
        RequestServerManager.getInstanceDIY().handleMethodDIY((GBaseActivity) getActivity(), null, isloadmore, null,
                Constant.getRootUrl() + Constant.Action.Action_MessageList, Constant.Action.Action_MessageList, RequestServerManager.getInstanceDIY().
                        getRequestParams(Constant.Action.Action_MessageList, App.getUserBean().getUserid()+"", App.getUserBean().getSession_id()+""), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        // JSONObject jsonObject = (JSONObject) result.getData();
                        List<MessageBean.DataEntity> messageBeanList = JSON.parseArray(result.getData().toString(), MessageBean.DataEntity.class);
                        if (messageBeanList.size()>0)
                        {
                            material_listview.setVisibility(View.VISIBLE);
                            linearLayoutEmpty.setVisibility(View.GONE);
                            fillData(messageBeanList);
                        }else
                        {
                            material_listview.setVisibility(View.GONE);
                            linearLayoutEmpty.setVisibility(View.VISIBLE);
                        }

                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {
                        material_listview.setVisibility(View.GONE);
                        linearLayoutEmpty.setVisibility(View.VISIBLE);
                    }
                });
    }
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId())
        {
            case R.id.relativeLayoutHDTZ:
                Intent intent=new Intent(MessageActivity.this,MessageHuoDongActivity.class);
                startActivity(intent);
                break;
            case R.id.relativeLayoutDDTX:
                Intent intent2=new Intent(MessageActivity.this,MessageOrderTixingActivity.class);
                startActivity(intent2);
                break;
            case R.id.relativeLayoutGGTZ:
                Intent intent3=new Intent(MessageActivity.this,MessageGongGaoActivity.class);
                startActivity(intent3);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void onEventMainThread(JpushCurrentMessageActivity jpushCurrentMessageActivity) {
        TaskGetMessageList(false);
    }
}
